package com.nervenets.shopping.controller.shopping;

import com.nervenets.general.entity.AdditionalCondition;
import com.nervenets.general.entity.ResponsePageResult;
import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.aspect.JwtRole;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.redis.RedisSyncLocker;
import com.nervenets.general.web.BaseEntityController;
import com.nervenets.general.web.params.BoolIdParams;
import com.nervenets.general.web.params.IdParams;
import com.nervenets.shopping.controller.shopping.params.ProductFormParams;
import com.nervenets.shopping.controller.shopping.params.ProductPagingParams;
import com.nervenets.shopping.hibernate.domain.shopping.Product;
import com.nervenets.shopping.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Api(value = "商品管理接口v1", tags = "商品管理接口")
@RestController
@RequestMapping("/api/product")
@JwtRole(group = "product", groupName = "商品管理", function = "product", functionName = "商品管理", queue = 400)
public class ProductController extends BaseEntityController<Product, ProductService, ProductFormParams, ProductPagingParams, Product, Product> {
    @Autowired
    private RedisSyncLocker<ResponseEntity<ResponseResult<Product>>> redisSyncLocker;

    @Override
    @ApiOperation(value = "列表")
    @PostMapping("/list")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponsePageResult<Product>> list(@Valid @RequestBody ProductPagingParams params, HttpServletRequest request, HttpServletResponse response) {
        return super.list(params, request, response);
    }

    @Override
    @ApiOperation(value = "详情")
    @PostMapping("/detail")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<Product>> detail(@Valid @RequestBody IdParams params, HttpServletRequest request, HttpServletResponse response) {
        return super.detail(params, request, response);
    }

    @ApiOperation(value = "商品启用或禁用")
    @PostMapping("/enable")
    @JwtSecurity(permission = "enable", permissionName = "禁用启用")
    public ResponseEntity<ResponseResult<Product>> enable(@Valid @RequestBody BoolIdParams params) throws Exception {
        return redisSyncLocker.idLock(params.getTargetId(), () -> {
            Product product = service.findOne(params.getTargetId(), "您操作的商品不存在");
            product.setEnable(params.isYes());
            service.save(product);
            return successMessage(product);
        });
    }

    @ApiOperation(value = "获取当前拼团的商品")
    @PostMapping("/current")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<Product>> current() {
        List<Product> products = service.findAll((predicates, root, query, builder) -> predicates.add(builder.isTrue(root.get("enable"))), 1);
        return successMessage(products.isEmpty() ? null : products.get(0));
    }
}
